package com.example.sls.dao.field;

import com.example.sls.pojo.field.FieldReviews;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 场地评价表(FieldReviews)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:55:03
 */
public interface FieldReviewsDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from field_reviews where id=#{id}")
  FieldReviews queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param fieldReviews 查询条件
   * @param pageSize     分页对象
   * @param offset       偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from field_reviews where 1=1" +
          " and (id=#{fieldReviews.id} or (isnull(#{fieldReviews.id}) or #{fieldReviews.id}=0))" +
          " and (field_id=#{fieldReviews.fieldId} or (isnull(#{fieldReviews.fieldId}) or #{fieldReviews.fieldId}=0))" +
          " and (user_id=#{fieldReviews.userId} or (isnull(#{fieldReviews.userId}) or #{fieldReviews.userId}=0))" +
          " and (rating=#{fieldReviews.rating} or (isnull(#{fieldReviews.rating}) or #{fieldReviews.rating}=0))" +
          " and (review=#{fieldReviews.review} or (isnull(#{fieldReviews.review}) or #{fieldReviews.review}=''))" +
          " and (created_at=#{fieldReviews.createdAt} or (isnull(#{fieldReviews.createdAt}) or #{fieldReviews.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<FieldReviews> queryAllByLimit(FieldReviews fieldReviews, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from field_reviews")
  long count();

  /**
   * 新增数据
   *
   * @param fieldReviews 实例对象
   * @return 影响行数
   */
  @Insert("insert into field_reviews (id,field_id,user_id,rating,review,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{fieldId}!=0 and !isnull(#{fieldId}),#{fieldId},null)," +
          "if(#{userId}!=0 and !isnull(#{userId}),#{userId},null)," +
          "if(#{rating}!=0 and !isnull(#{rating}),#{rating},null)," +
          "if(#{review}!='' and !isnull(#{review}),#{review},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(FieldReviews fieldReviews);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param fieldReviews 实例对象
   * @return 影响行数
   */
  @Update("update field_reviews set " +
          "field_id=if(isnull(#{fieldId}) or #{fieldId}=0,field_id,#{fieldId})," +
          "user_id=if(isnull(#{userId}) or #{userId}=0,user_id,#{userId})," +
          "rating=if(isnull(#{rating}) or #{rating}=0,rating,#{rating})," +
          "review=if(isnull(#{review}) or #{review}='',review,#{review})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(FieldReviews fieldReviews);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from field_reviews where id=#{id}")
  int deleteById(Integer id);
}

